//
//  RootViewController.swift
//  GZB
//
//  Created by lxl on 2019/6/7.
//  Copyright © 2019 GZB. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class RootViewController: UIViewController {
    //中间文字
    var str_title : String? = nil
    //左右侧按钮文字
    var leftTitle    : String? = nil
    var rightTitle   : String? = nil
    var rightImg     : String? = nil
    var leftBtn      : UIButton? = nil
    var righttBtn    : UIButton? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
//        RootViewController.getTheStringFromHtml(htmlString: <#T##String#>)
        // Do any additional setup after loading the view.
        view.backgroundColor = KYP_f2f2f2_COLOR

        self.navigationController?.navigationBar.isTranslucent = false;
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:KYP_333333_COLOR, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18)]
        
        
//        let navBarHairlineImageView = PublicClass.findHairlineImageViewUnder(self.navigationController?.navigationBar)
//        navBarHairlineImageView?.isHidden = true
        
        //设置导航栏
        setNavBar()
        createIQKeyboardManager()
    }
    func createIQKeyboardManager() -> Void {
        //控制点击背景是否收起键盘
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        //控制键盘上的工具条文字颜色是否用户自定义
        IQKeyboardManager.shared.shouldShowToolbarPlaceholder = true
        //       IQKeyboardManager.shared.shouldToolbarUsesTextFieldTintColor = true
        //将右边Done改成完成
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "完成"
        // 控制是否显示键盘上的工具条
        IQKeyboardManager.shared.enableAutoToolbar = false
        //最新版的设置键盘的returnKey的关键字 ,可以点击键盘上的next键，自动跳转到下一个输入框，最后一个输入框点击完成，自动收起键盘
        IQKeyboardManager.shared.toolbarManageBehaviour = .byPosition
        IQKeyboardManager.shared.keyboardDistanceFromTextField = 0
        IQKeyboardManager.shared.enable = true; // 控制整个功能是否启用
        
    }
    //MARK:- 设置导航栏
    func setNavBar() {
        if !isRootVC() || isPresentedVC(){//需要返回按钮的
            leftBtn = PublicTools.SLeftNavBarButtonWith(title: leftTitle, str_img: "back", action: #selector(backToTop), controller: self)
        }
        if rightTitle != nil || rightImg != nil {
            righttBtn = PublicTools.SRightNavBarButtonWith(title: rightTitle, str_img: rightImg, action:  #selector(rigthBtnClick), controller: self)
        }
        
        if str_title != nil {
            self.navigationItem.title = str_title
        }
    }
    //MARK:- 导航右侧按钮点击事件
    @objc func rigthBtnClick() -> Void {
        
    }
    //MARK:-导航左侧返回事件
    @objc func backToTop() -> Void {
        if isPresentedVC() {
            self.dismiss(animated: true, completion: nil)
        }else{
            self.navigationController?.popViewController(animated: true)
        }
    }
    func jumpTheResponseVC(tagetVC:String,title:String) -> Void {
        if !tagetVC.isEmpty {
            let vcClass = NSClassFromString("maosheng." + tagetVC) as! RootViewController.Type
            let vc = vcClass.init()
            self.jumpNextVC(jumpVC: vc, title: title)
        }
        
    }
    func jumpNextVC(jumpVC:RootViewController,title:String? = nil) -> Void {
        if title != nil {
            jumpVC.str_title = title
        }
        jumpVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(jumpVC, animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    //MARK:-判断是那种方式进来的
    func isPresentedVC() -> Bool {
        return self.presentedViewController != nil
    }
    func isRootVC() -> Bool {
        return (self == self.navigationController?.viewControllers.first)
    }

}
